<template>
  <!--入库列表  唐美莉 -->
  <section>
    <table border="0" width="100%" style="border-spacing: 0; border-collapse: collapse">
      <tr>
        <td>
          <el-form :inline="true">
            <el-form-item>
              <el-select v-model="form.inOutWay" clearable filterable placeholder="请选择入库方式">
                <el-option label="全部" :key="-1" value></el-option>
                <el-option v-for="(item, index) in typeList" :key="index" :value="item.id" :label="item.desc"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input v-model="form.drugName" clearable placeholder="药品名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="form.lotNumber" clearable placeholder="药品批次号"></el-input>
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="form.putInStorageStartTime"
                type="date"
                :editable="true"
                value-format="yyyy-MM-dd"
                :picker-options="pickerOptions"
                placeholder="入库开始时间"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="form.putInStorageEndTime"
                type="date"
                :editable="true"
                value-format="yyyy-MM-dd"
                :picker-options="pickerOptions"
                placeholder="入库结束时间"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" circle icon="el-icon-search" @click="getData(-1)"></el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :disabled="!qxBtns.add" plain @click="addDurg(null)">+添加</el-button>
            </el-form-item>
          </el-form>
        </td>
        <td style="text-align: right; vertical-align: top">
          <el-button @click="exportExcel()" plain>导出</el-button>
        </td>
      </tr>
    </table>
    <table class="table-box">
      <thead>
        <tr>
          <th>入库单号</th>
          <th>入库日期</th>
          <th>入库方式</th>
          <th>供应商</th>
          <th>制单人</th>
          <th>状态</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in tableList" :key="index">
          <td>{{ item.stockCode }}</td>
          <td>{{ item.putInStorageDate | changeTime }}</td>
          <td>{{ item.inOutWay | changeAssist(typeList) }}</td>
          <td>{{ item.supplierName }}</td>
          <td>{{ item.memberName }}</td>
          <td>{{ item.status == 1 ? "正常" : "待审核" }}</td>
          <td v-if="item.status == 2">
            <el-link v-if="qxBtns.update" @click="addDurg(item)" :underline="false" type="primary">修改</el-link>&nbsp;
            <el-link v-if="qxBtns.audit" type="primary" :underline="false" @click="getDetail(item)">审核</el-link>&nbsp;
            <el-link v-if="qxBtns.delete" @click="onDel(item)" :underline="false" type="danger">删除</el-link>
          </td>
          <td v-if="item.status !== 2"><el-link v-if="qxBtns.audit" type="primary" :underline="false" @click="getDetail(item)">查看详情</el-link>&nbsp;</td>
        </tr>
      </tbody>
    </table>
    <div v-if="tableList.length == 0" style="color: #999; padding: 10px 0; text-align: center">
      <i class="iconfont iconwushuju" style="font-size: 40px"></i>
      <div>暂无数据</div>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <!-- <set v-model="dialogEdit" :curItem="curItem" @success="getData"></set> -->
  </section>
</template>
<script>
import Excel from "@/_js/excel.js";
export default {
  name: "storageList",
  data() {
    return {
      dialogJoin: false,
      dialogEdit: false,
      curItem: null,
      pager: { pageSize: 10, pageNum: 1, total: 0 },
      tableList: [],
      form: {
        hpId: "",
        putInStorageStartTime: "", //入库时间
        putInStorageEndTime: "", //出库时间
        type: 1,
        inOutWay: "",
        drugName: "", //药品名称
        lotNumber: "", //批号
        source: 1,
        status: "",
      },
      pickerOptions: {
        disabledDate: (time) => {
          return time.getTime() > Date.now();
        },
      },
      typeList: [],
      qxBtns: {},
      dataList: [],
      distype: "2",
      setType: "1",
    };
  },
  created() {
    let th = this;
    th.form.hpId = th.adminUser.hpId;
    th.getQX();
    th.getData();
    th.getBasicData();
  },
  methods: {
    onJoin(obj) {
      this.dialogJoin = true;
      this.curItem = obj;
    },
    onDel(obj, index) {
      let th = this;
      th.$confirm(`确定删除吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          let j = { ids: obj.id };
          th.showLoading();
          th.$api.delMedicalStock(j).then((res) => {
            if (res.executed) {
              th.$message.success("删除成功");
              th.getData();
            } else {
              th.$message.error(res.message);
            }
            th.showLoading(false);
          });
        })
        .catch(() => {});
    },
    getData(type, id) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      if (id != undefined) {
        if (id == -1) {
          th.form.drugTypeId = "";
        } else {
          th.form.drugTypeId = id;
        }
      }
      let j = {};
      Object.assign(j, th.pager);
      Object.assign(j, th.form);
      th.showLoading();
      th.$api.getMedicalStockList(j).then((res) => {
        if (res.executed) {
          if (res.dataBody != undefined) {
            th.tableList = res.dataBody.list;
            th.pager.total = res.dataBody.total;
          } else {
            th.tableList = [];
            th.pager.total = 0;
          }
        } else {
          th.$message.error(res.message);
        }
        th.showLoading(false);
      });
    },
    addDurg(obj) {
      let th = this;
      let j = {};
      if (obj != null) {
        j = {
          id: th.jiaMi(obj.id),
        };
      } else {
        j = {
          id: th.jiaMi(0),
        };
      }
      th.$router.push({
        path: "/page/eidtDrug",
        query: th.getUrlToken(j),
      });
    },
    getDetail(obj) {
      let th = this;
      let j = {};
      if (obj != null) {
        j = {
          id: th.jiaMi(obj.id),
          type: 2,
        };
      } else {
        j = {
          id: th.jiaMi(0),
          type: 2,
        };
      }
      let js = th.$utils.JsonToUrl(th.getUrlToken(j));
      th.addNavMenu("药品入库详情", "/page/drugDetail?" + js, "");
      th.$router.push({
        path: "/page/drugDetail",
        query: th.getUrlToken(j),
      });
    },
    getBasicData() {
      let th = this;
      th.getCommonConfigByKey("入库方式").then((res) => {
        th.typeList = res;
      });
    },
    exportExcel() {
      let th = this;
      let j = {};
      Object.assign(j, th.pager);
      Object.assign(j, th.form);
      j.pageSize = "1000";
      th.showLoading(true);
      th.$api.getMedicalStockList(j).then((res) => {
        if (res.executed) {
          if (res.dataBody != undefined) {
            let data = res.dataBody.list;
            let type = [];
            th.typeList.forEach((item, index) => {
              type[item.id] = item.desc;
            });
            Excel(
              data,
              [
                { key: "stockCode", name: "入库单号" },
                { key: "putInStorageDate", name: "入库日期" },
                { key: "inOutWay", name: "入库方式", enum: type, type: "enum" },
                { key: "supplierName", name: "供应商" },
                { key: "memberName", name: "制单人" },
              ],
              "药品入库列表"
            );
          } else {
          }
        } else {
          th.$message.error(res.message);
        }
        th.showLoading(false);
      });
    },
  },
  components: {},
};
</script>
